﻿$(document).ready(function () {
    var timeToHide = 18;

    $(".list-group-item").click(function (e) {
        var currentTime = $(this).attr("id");
        e.preventDefault();
        $('#bookingTitle').text("Rumsbokning: " + $(this).text());

        $('#ddFrom').val(currentTime);
        $('#tBoxFrom').val($('#ddFrom option:selected').text());
        currentTime++;
        $('#ddTo').val(currentTime);
        $('#tBoxTo').val($('#ddTo option:selected').text());

        for (var i = 0; i <= timeToHide; i++) {
            $("#ddTo option[value='" + i + "']").show();
            $("#ddTo option[value='" + i + ",5']").show();
        }

        timeToHide = parseInt($("#ddFrom").val());
        for (var i = 0; i <= timeToHide; i++) {
            $("#ddTo option[value='" + i + "']").hide();
            if (i != timeToHide)
                $("#ddTo option[value='" + i + ",5']").hide();
        }
        timeToHide = 18;
    });

    $('#ddFrom').change(function () {
        for (var i = 0; i <= timeToHide; i++) {
            $("#ddTo option[value='" + i + "']").show();
            $("#ddTo option[value='" + i + ",5']").show();
        }

        timeToHide = parseInt($("#ddFrom").val());
        for (var i = 0; i <= timeToHide; i++) {
            $("#ddTo option[value='" + i + "']").hide();
            if(i != timeToHide)
            $("#ddTo option[value='" + i + ",5']").hide();
        }
        timeToHide = 18;
    });

    $("#editLink").click(function () {
        $('#ddFromEdit').val($(this).attr('data-start'));
        $('#tBoxFromEdit').val($('#ddFromEdit option:selected').text());
        $('#ddToEdit').val($(this).attr('data-end'));
        $('#tBoxToEdit').val($('#ddToEdit option:selected').text());
    });

    $(".delBooking").click(function () {
        $('#delTitle').text("Ta bort bokning för " + $(this).attr("data-name"));
        var id = $(this).closest(".booking").attr("id").replace('booking_', '');
        $('#delId').val(id);
    });

    $("select").change(function () {
        $("#" + $(this).attr("data-name")).val($("#" + $(this).attr("id") + " option:selected").text());
    });
});